package com.jason.www.activity;

import android.util.Log;
import android.view.MotionEvent;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jason.www.R;
import com.jason.www.base.BaseActivity;

import butterknife.BindView;

public class TouchEventActivity extends BaseActivity {
    @BindView(R.id.myFramelayout)
    FrameLayout frameLayout;
    @BindView(R.id.myLinearLayout)
    LinearLayout linearLayout;
    @BindView(R.id.myTextView)
    TextView textView;
    float lastX = 0.0f;
    float lastY = 0.0f;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_touch_event;
    }

    @Override
    protected void initView() {
        super.initView();
        textView.setOnClickListener(v -> {
            Log.d("toucheventlog", "textview被点击了");
            showToast("点击了TextView");
        });
        linearLayout.setOnTouchListener((v, event) -> {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    Log.d("toucheventlog", "linearLayout-->OnTouchListener-->onTouch()：down");
                    lastX = event.getRawX();
                    lastY = event.getRawY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (lastX == 0.0f || lastY == 0.0f) {
                        lastX = event.getRawX();
                        lastY = event.getRawY();
                    }
                    float nowX = event.getRawX();
                    float nowY = event.getRawY();
                    float movedX = nowX - lastX;
                    float movedY = nowY - lastY;
                    linearLayout.setX(linearLayout.getX() + movedX);
                    linearLayout.setY(linearLayout.getY() + movedY);
                    lastX = nowX;
                    lastY = nowY;
                    Log.d("toucheventlog", "linearLayout-->OnTouchListener-->onTouch()：move:" + movedX + "," + movedY);
                    break;
                case MotionEvent.ACTION_UP:
                    lastX = 0.0f;
                    lastY = 0.0f;
                    Log.d("toucheventlog", "linearLayout-->OnTouchListener-->onTouch()：up");
                    break;
                default:
                    break;
            }
            return true;
        });
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.d("toucheventlog", "TouchEventActivity-->dispatchTouchEvent()：down");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.d("toucheventlog", "TouchEventActivity-->dispatchTouchEvent()：move");
                break;
            case MotionEvent.ACTION_UP:
                Log.d("toucheventlog", "TouchEventActivity-->dispatchTouchEvent()：up");
                break;
            case MotionEvent.ACTION_CANCEL:
                Log.d("toucheventlog", "TouchEventActivity-->dispatchTouchEvent()：cancel");
                break;
            default:
                break;
        }
        return super.dispatchTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.d("toucheventlog", "TouchEventActivity-->onTouchEvent()：down");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.d("toucheventlog", "TouchEventActivity-->onTouchEvent()：move");
                break;
            case MotionEvent.ACTION_UP:
                Log.d("toucheventlog", "TouchEventActivity-->onTouchEvent()：up");
                break;
            case MotionEvent.ACTION_CANCEL:
                Log.d("toucheventlog", "TouchEventActivity-->onTouchEvent()：cancel");
                break;
            default:
                break;
        }
        return super.onTouchEvent(event);
    }
}